﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FineUI;

public partial class Manager_Login : System.Web.UI.PageEx
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
            btnReset.OnClientClick = SimpleForm1.GetResetReference();
        }
    }
    private void LoadData()
    {
        InitCaptchaCode();
    }

    /// <summary>
    /// 初始化验证码
    /// </summary>
    private void InitCaptchaCode()
    {
        // 创建一个 6 位的随机数并保存在 Session 对象中
        HttpContext.Current.Session["CaptchaImageText"] = GenerateRandomCode();
        imgVaildCode.ImageUrl = "~/Basic/VaildCodeImage.ashx?w=150&h=30&t=" + DateTime.Now.Ticks;
    }
    /// <summary>
    /// 创建一个 6 位的随机数
    /// </summary>
    /// <returns></returns>
    private string GenerateRandomCode()
    {
        string s = String.Empty;
        Random random = new Random();
        for (int i = 0; i < 6; i++)
        {
            s += random.Next(10).ToString();
        }
        return s;
    }
    /// <summary>
    /// 重新加载验证码
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        InitCaptchaCode();
    }
    /// <summary>
    /// 提交登录验证
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (txtVaildCode.Text != Session["CaptchaImageText"].ToString())
        {
            Alert.ShowInTop("验证码错误！");
            return;
        }
        Model.DbManager manager = null;
        manager = new Model.DbManager();
        string strLoginRefMsg = string.Empty;
        string strUserName=txtUserName.Text.Trim();
        string strPwd = txtPwd.Text.Trim();
        bool bLoginFlag = DalEx.DbManagerEx.Login(strUserName, strPwd, ref manager, ref strLoginRefMsg);
        if (bLoginFlag)
        {
            UserType = 0;
            UserId = manager.MId;
            Session["UserId"] = UserId.ToString();
            Session["UserName"] = UserName = manager.MUser.ToString();


            Model.DbActionLog actionLog = new Model.DbActionLog();
            actionLog.AUId = UserId.ToString();
            actionLog.ADateTime = DateTime.Now;
            actionLog.ANote = "用户(" + UserName + ")登录系统！";
            actionLog.AIpaddress = WebBase.GetClientIP.getIPAddress2Num().ToString();
            actionLog.AOS = WebBase.Comm.GetOS;
            actionLog.ABrowse = WebBase.Comm.GetUserBrowser;
            Dal.DbActionLogDal.Insert(actionLog);

            Response.Redirect("Main.aspx");
        }
        else
        {
            Alert.ShowInTop(strLoginRefMsg);
        }
    }
}
